import { AppModel } from '../../../../model/AppModel'
import { createGPIOMenu } from '../BlockPackage'


export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model: AppModel) {
        this.appendDummyInput()
            .appendField("数字输出")
            .appendField(createGPIOMenu(model, true), 'gpio')
            .appendField(new Blockly.FieldDropdown(() => {
                return [
                    ["高电平", '1'] ,
                    ["低电平", '0'] ,
                ]
            }), 'value')
    } ,

    run (args: any, util: any, runtime: any, model:AppModel) {
        let stud = model.masterPart.findStudFromName(args.gpio)
        if(!stud)
            return
        stud.gpio.value = args.value=='1'? 1: 0
    } ,

    coder: {
        JavaScript(block: any, coder: any) {

            let pin = block.getFieldValue('gpio')
            let value = block.getFieldValue('value')

            return `${coder.indentString()}digitalWrite(${pin}, ${value})\n`
        }
    }
}
